package com.leslie.hhcommunity.controller;

import com.leslie.hhcommunity.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

/**
 * @author
 * @PackageName:com.leslie.hhcommunity.controller
 * @ClassnName:DataContoller
 * @Description:
 * @Date 2022/1/12  15:59
 */
@Controller
@RequestMapping("/data")
public class DataContoller {

	@Autowired
	private DataService dataService;

	/**
	 * 统计的页面
	 *
	 * @return
	 */
	@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
	public String getPage() {
		return "site/admin/data";
	}

	/**
	 * 统计网站UV
	 *
	 * @param start
	 * @param end
	 * @return
	 */
	@RequestMapping(value = "/uv", method = RequestMethod.POST)
	public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
						@DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model) {

		long uv = dataService.calculateUV(start, end);
		model.addAttribute("uvResult", uv);
		model.addAttribute("uvStartDate", start);
		model.addAttribute("uvEndDate", end);
		return "forward:/data";
	}


	/**
	 * 统计活跃用户
	 * @param start
	 * @param end
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dau", method = RequestMethod.POST)
	public String getDAU(
			@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
			@DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
			Model model) {
		long dau = dataService.calculateDAU(start, end);
		model.addAttribute("dauResult", dau);
		model.addAttribute("dauStartDate", start);
		model.addAttribute("dauEndDate", end);
		return "forward:/data";
	}

}
